package com.baidu.customview.widget.progressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.baidu.customview.R;

/**
 * Created by JGW-android on 2016/3/4.
 */
public class AddProgress extends View {
    private int ARC_WIDTH = 5;
    private int PADDING = 20;
    private int mProgress = 0;
    private final int START = -2;
    private final int STOP = -1;
    private int cx;
    private int cy;
    private int innerRadius;
    private Paint circlePaint;
    private Paint arcPaint;
    private RectF rectF;
    private Handler handler;

    public AddProgress(Context context) {
        super(context);
    }

    public AddProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setColor(getResources().getColor(R.color.white));

        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);//default FILL
        arcPaint.setStrokeWidth(ARC_WIDTH);
        arcPaint.setColor(getResources().getColor(R.color.pink));


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case START:
                        setProgress();
                        break;
                    case STOP:
                        break;
                }
            }
        };
    }

    private synchronized void setProgress() {
        mProgress++;
        postInvalidate();
        if (mProgress == 100) {
            mProgress = 0;
        }
    }

    private void initDimension() {
        cx = getWidth() / 2;
        cy = getHeight() / 2;
        innerRadius = (cx < cy ? cx : cy) - PADDING;
        rectF = new RectF(cx - innerRadius - ARC_WIDTH, cy - innerRadius - ARC_WIDTH, cx + innerRadius + ARC_WIDTH, cy + innerRadius + ARC_WIDTH);
    }

    public AddProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(cx, cy, innerRadius + 2, circlePaint);
        canvas.drawCircle(cx, cy, innerRadius + ARC_WIDTH, circlePaint);
        canvas.drawArc(rectF, 270, (float) (3.6 * mProgress), false, arcPaint);
    }

    public void start() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(40);
                        handler.sendEmptyMessage(START);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        initDimension();
    }
}
